package com.example.comeon.slice;

import com.example.comeon.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class MainAbilitySlice extends AbilitySlice {


    static HiLogLabel mLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");

    private Text mText;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);


        mText = (Text) findComponentById(ResourceTable.Id_text_helloworld);


        getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(new Runnable() {
            @Override
            public void run() {


                try {
                    // 要接收的报文
                    byte[] bytes = new byte[1024];
                    DatagramPacket packet = new DatagramPacket(bytes, bytes.length);

                    // 创建socket并指定端口
                    DatagramSocket socket = new DatagramSocket(5678);

                    while (true) {
                        // 接收socket客户端发送的数据。如果未收到会一致阻塞
                        socket.receive(packet);
                        String receiveMsg = new String(packet.getData(),0,packet.getLength());
                        System.out.println("packet:" + packet.getLength());
                        System.out.println("packet:" + receiveMsg);

                        getMainTaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {

                                long number = Long.parseLong(receiveMsg.substring(0, 6));

                                float temp = ((float)(number / 100)) / 100;
                                long humi = number % 100;
                                mText.setText("温度:" + temp + " 湿度:" + humi);
                            }
                        });

                    }
                    // 关闭socket
                    // socket.close();
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

                /*
                try {
                    // 和服务器创建连接
                    Socket socket = new Socket("192.168.30.196",5678);

                    // 要发送给服务器的信息
                    OutputStream os = socket.getOutputStream();
                    PrintWriter pw = new PrintWriter(os);
                    pw.write("客户端发送信息");
                    pw.flush();

                    // socket.shutdownOutput();

                    // 从服务器接收的信息
                    InputStream is = socket.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String info = null;
                    while((info = br.readLine())!=null){
                        System.out.println("我是客户端，服务器返回信息："+info);
                    }

                    br.close();
                    is.close();
                    os.close();
                    pw.close();
                    socket.close();
                } catch (Exception e) {
                    System.out.println("Exception："+e);

                    HiLog.info(mLabel, "Exception："+e);
                    e.printStackTrace();
                }


                 */
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
